JQuery Validation [migrated]

Posted by user41354 on Programmers See other posts from Programmers or by user41354
Published on 2011-11-23T15:43:54Z Indexed on 2011/11/23 18:04 UTC
Read the original article Hit count: 142

Filed under:
|

Im trying to get my form to validate...so basically its working, but a little bit too well, I have two text boxes, one is a start date, the other an end date in the format of mm/dd/yyyy

if the start date is greater than the end date...there is an error if the end date is less than the start date...there is an error if the start date is less than today's date...there is an error

The only thing is when I correct the error, the error warning is still there...here is my code:

    dates.change(function () {
        var testDate = $(this).val();
        var otherDate = dates.not(this).val();
        var now = new Date();
        now.setHours(0, 0, 0, 0);

        // Pass Dates
        if (testDate != '' && new Date(testDate) < now) {
            addError($(this));
            $('.flightDateError').text('* Dates cannot be earlier than today.');
            isValid = false;
            return;
        }

        // Required Text 
        if ($(this).hasClass("FromCal") && testDate == '') {
            addError($(this));
            $('.flightDateError').text('* Required');
            isValid = false;
            return;
        }
        // Validate Date
        if (!isValidDate(testDate)) {
            // $(this).addClass('validation_error_input');  
            addError($(this));
            $('.flightDateError').text('* Invalid Date');
            isValid = false;
            return;
        }
        else {
            // $(this).removeClass('validation_error_input');
            removeError($(this));
            if (!dates.not(this).hasClass('validation_error_input'))
                $('.flightDateError').text(' ');
        }

        // Validate Date Ranges
        if ($(this).val() != '' && dates.not(this).val != '') {
            if ($(this).hasClass("FromCal")) {
                if (new Date(testDate) > new Date(otherDate)) {
                    addError($(this));
                    $('.flightDateError').text('* Start date must be earlier than end date.');
                    isValid = false;
                    return;
                }
            }
            else{
                if (new Date(testDate) < new Date(otherDate)) {
                    addError($(this));
                    $('.flightDateError').text('* End date must be later than start date.');
                    return;
                }
            }
        }
    });

The main Issue is this part, I believe

 // Validate Date Ranges
            if ($(this).val() != '' && dates.not(this).val != '') {
                if ($(this).hasClass("FromCal")) {
                    if (new Date(testDate) > new Date(otherDate)) {
                        addError($(this));
                        $('.flightDateError').text('* Start date must be earlier than end date.');
                        isValid = false;
                        return;
                    }
                }
                else{
                    if (new Date(testDate) < new Date(otherDate)) {
                        addError($(this));
                        $('.flightDateError').text('* End date must be later than start date.');
                        return;
                    }
                }
            }

testDate is the start date
otherDate is the end date

Thanks in advanced, J

© Programmers or respective owner

Related posts about jQuery

Related posts about validation